package com.opoc.service.Impl;

import com.opoc.mapper.SystemLogMapper;
import com.opoc.pojo.Log;
import com.opoc.pojo.Log_sql;
import com.opoc.service.SystemLogService;
import com.opoc.utils.TimeUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@Service
public class SystemLogServiceImpl implements SystemLogService {

  @Autowired
  SystemLogMapper systemLogMapper;
  @Override
  public void addLog(String system_module, String operation_kind, Integer admin_id, String operation_state) {
    Log_sql log_sql = new Log_sql();
    log_sql.setSystem_module(system_module);
    log_sql.setAdmin_id(admin_id);
    log_sql.setOperation_kind(operation_kind);
    log_sql.setOperation_state(operation_state);
    log_sql.setOperation_date(TimeUtils.getTimeStamp());

    systemLogMapper.insert(log_sql);
  }

  @Override
  public List<Log> getLogs(String operation_kind, String start_time, String end_time) throws ParseException {
    List<Log_sql> log_sqls = systemLogMapper.getLogs(operation_kind, TimeUtils.StringDateToSqlDate(start_time), TimeUtils.StringDateToSqlDate(end_time));
    if (log_sqls == null){
      return null;
    }
    List<Log> logs = new ArrayList<>();
    for (Log_sql log_sql : log_sqls){
      Log log = new Log();
      BeanUtils.copyProperties(log_sql, log, "operation_date");
      log.setOperation_date(TimeUtils.SqlDateToStringDate(log_sql.getOperation_date()));
      logs.add(log);
    }
    return logs;
  }

  @Override
  public void deleteLogs(List<Integer> ids) {
    ids.forEach(id -> {
      systemLogMapper.deleteById(id);
    });
  }
}
